package com.avengers.mobile.layer.rest;


import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import javax.ws.rs.PathParam;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;




import com.avengers.mobile.layer.model.Usuario;
import com.avengers.mobile.layer.service.UserService;
import com.google.gson.Gson;



@Repository("restUsuario")
@Path("/users")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
public class UsuarioRest {

	static final Log log = LogFactory.getLog(UsuarioRest.class);

	@Autowired
	UserService userService;
	
	Gson json = new Gson();	
	
	@Autowired
	Usuario usuario;
  				
	
	@POST
	@Path("/{usuarioID}")	
	@Produces(MediaType.APPLICATION_JSON)
	public Response findUser(@PathParam("usuarioID") int id) throws Exception { 	
		return Response.ok(json.toJson(userService.findUsuarioById(id))).build();		
		
	}
	

	@GET	
	@Path("/validateUser/{usuario}/{password}")	
	@Produces(MediaType.APPLICATION_JSON)
	public Response ValidateUser(@PathParam("usuario") String user,@PathParam("password") String password) throws Exception {					
		
		Object[] params = new Object[2];
		params[0] =user;
		params[1] =password;
		
		
		usuario=new Usuario();
		usuario=userService.validaLogin(params);	
		
		if(usuario==null){
			return Response.status(Status.INTERNAL_SERVER_ERROR).entity("Usuario y/o password incorrectos").build();
		}
		usuario.setPassword(null);
		
		return Response.ok(json.toJson(usuario)).build();		
	}

}



